<?php
include "Sprint.php";

class SprintDAO extends DataLayer {
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;	
	}

	public function getSprints($id=0, $idProject, $page=0){
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							s.id_sprint, s.id_equipe, s.id_projeto, s.ordem, s.status, 
							DATE_FORMAT(s.data_inicial, '%d/%m/%Y') data_inicial,
							DATE_FORMAT(s.data_final, '%d/%m/%Y') data_final,
							(SELECT COUNT(id_tarefa) FROM tarefas WHERE status <> 0 and id_sprint = s.id_sprint) tasks,
							s.data_inicial completeDateInit, s.data_final completeDateEnd, e.nome team
						FROM 
							sprints s
						LEFT JOIN
							equipes e on e.id_equipe = s.id_equipe
						WHERE 
							s.status <> 0 and s.id_projeto = ".$idProject;

		if($id > 0)						
			$this->sql .= " and s.id_sprint = ".$id;
		
		$this->sql .= " ORDER BY s.ordem DESC";

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}
		
		$this->result = parent::select($this->sql);
		$sprintsList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$sprintsList[$i] = new Sprint();
				$sprintsList[$i]->setDadosDB($row);

				$i++;
			}
		}
		
		return $sprintsList;
	}

	public function nextSprint($idProject){
		$this->sql ="	SELECT 
							MAX(s.ordem) ordem
						FROM 
							sprints s
						WHERE 
							s.id_projeto = ".$idProject;
		
		$this->result = parent::select($this->sql);

		if(count($this->result) > 0){
			$order = $this->result[0]->ordem;

			if($order == null)
				return 0;
			else
				return $order+1;
		}

		return 0;
	}

	public function actualSprint($idProject){
		$this->sql ="	SELECT 
							s.id_sprint, s.ordem, DATE_FORMAT(s.data_final, '%d/%m/%Y') data_final
						FROM 
							(SELECT MIN(ordem) ordem FROM sprints WHERE id_projeto = ".$idProject." AND status = 1) so,
							sprints s
						WHERE 
							s.ordem = so.ordem AND s.id_projeto = ".$idProject;

		$result = parent::select($this->sql);

		$sprint = new Sprint();

		if(count($result) > 0){
			$sprint->setDadosDB($result[0]);
		}else{
			$this->sql ="	SELECT 
								s.id_sprint, s.ordem, DATE_FORMAT(s.data_final, '%d/%m/%Y') data_final
							FROM 
								(SELECT MAX(ordem) ordem FROM sprints WHERE id_projeto = ".$idProject." AND status IN (1,2)) so,
								sprints s
							WHERE 
								s.ordem = so.ordem AND s.id_projeto = ".$idProject;

			$result = parent::select($this->sql);
			$sprint->setDadosDB($result[0]);
		}

		return $sprint;
	}

	public function save(Sprint $obj){
		if($obj != NULL){
			$dbObj = $obj->fieldsDBObj;
			unset($dbObj["id_sprint"]);
			if($obj->id > 0){
				parent::update("sprints",$dbObj,"id_sprint = ".$obj->id);
				return $obj->id;
			}else{
				return parent::insert("sprints",$dbObj,"data");
			}
		}else{
			return 0;
		}
	}

	public function setEndSprint($idProject, $idSprint){
		if($idSprint > 0){
			$sprint = new Sprint();
			$sprint->status = 2;

			parent::update("sprints",$sprint->fieldsDBObj,"id_sprint = ".$idSprint);
			
			$this->sql = "	SELECT 
								*
							FROM
								sprints
							WHERE
								status = 1 AND id_projeto = ".$idProject;
			
			$this->result = parent::select($this->sql);

			if(count($this->result) == 0){
				$objProjectDAO = new ProjectDAO($this->lk);
				$objProjectDAO->setEndProject($idProject, false);
			}

			return true;
		}

		return false;
	}

	public function getTaskSprints($idProject){
		$this->sql ="	SELECT 
							s.id_sprint id, (select count(id_tarefa) from tarefas t where t.status <> 0 and t.id_sprint = s.id_sprint) tasks
						FROM 
							sprints s
						WHERE 
							s.status <> 0 and s.id_projeto = ".$idProject;
		
		$this->result = parent::select($this->sql);

		return $this->result;
	}

	public function deleteSprint($id, $idProject){
		if($id > 0){
			$rsSprints = $this->getSprints($id, $idProject);

			if(count($rsSprints) > 0){
				if($rsSprints[0]->tasks > 0){
					return -1;
				}else{
					$dados["status"] = 0;
					return parent::update("sprints", $dados, "id_sprint=".$id);
				}				
			}
		}

		return 0;
	}
}

?>